package com.learner.course.service.impl;

import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.course.repository.CourseDetailRepository;
import com.learner.course.service.CourseDetailService;
import com.learner.pojo.course.CourseDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Description: 添加课程详细概述的 逻辑处理 实现类
 * @Author: ice2020x
 * @Date: 2021/11/12
 */
@Service
public class CourseDetailServiceImpl implements CourseDetailService {


    @Autowired
    CourseDetailRepository courseDetailRepository;


    @Override
    public void save(CourseDetail courseDetail) {
        courseDetailRepository.save(courseDetail);
    }

    /**
     * @Description: 根据课程id查询课程概述信息
     * @Author: ice2020x
     * @Date: 2021/11/13
     */
    @Override
    public List<Map<String, String>> getCourseDetailByCourseId(String courseId) {
        Optional<CourseDetail> courseDetailOptional = courseDetailRepository.findById(courseId);
        if (!courseDetailOptional.isPresent()) {
            throw ExceptionUtil.createException(RestException.class, 2000002);
        }
        CourseDetail courseDetail = courseDetailOptional.get();
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map = null;
        if (!StringUtils.isEmpty(courseDetail.getDescription())) {
            map = new HashMap<>(2);
            map.put("title", "课程介绍");
            map.put("value", courseDetail.getDescription());
            list.add(map);
        }
        if (!StringUtils.isEmpty(courseDetail.getOverview())) {
            map = new HashMap<String, String>(2);
            map.put("title", "课程概述");
            map.put("value", courseDetail.getOverview());
            list.add(map);
        }
        if (!StringUtils.isEmpty(courseDetail.getTarget())) {
            map = new HashMap<>(2);
            map.put("title", "授课目标");
            map.put("value", courseDetail.getTarget());
            list.add(map);
        }
        if (!StringUtils.isEmpty(courseDetail.getAp())) {
            map = new HashMap<>(2);
            map.put("title", "先修知识");
            map.put("value", courseDetail.getAp());
            list.add(map);
        }
        if (!StringUtils.isEmpty(courseDetail.getProblem())) {
            map = new HashMap<>(2);
            map.put("title", "常见问题");
            map.put("value", courseDetail.getProblem());
            list.add(map);
        }
        if (!StringUtils.isEmpty(courseDetail.getResources())) {
            map = new HashMap<>(2);
            map.put("title", "参考资料");
            map.put("value", courseDetail.getResources());
            list.add(map);
        }
        return list;
    }


}
